package com.javaweb.controller;

import com.javaweb.service.CategoryService;
import com.javaweb.service.dto.CategoryDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/blog-api/v0.1/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    // 删除分类
    @GetMapping("/delete")
    public ResponseEntity<Void> delete(@RequestParam String id) {
        categoryService.deleteCategory(id);
        return ResponseEntity.ok().build();
    }

    // 新增分类
    @PostMapping("/add")
    public ResponseEntity<CategoryDto> add(@RequestBody CategoryDto categoryDto) {
        CategoryDto createdCategory = categoryService.addCategory(categoryDto);
        return ResponseEntity.ok(createdCategory);
    }

    // 获取所有分类
    @GetMapping("/list")
    public ResponseEntity<List<CategoryDto>> list() {
        List<CategoryDto> categories = categoryService.listCategories();
        return ResponseEntity.ok(categories);
    }
}
